package com.challenger.teacher.action;

import java.util.HashMap;
import java.util.List;

import com.challenger.teacher.entity.Teacher;
import com.challenger.teacher.entity.TeacherUsr;
import com.challenger.teacher.services.TeacherService;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 教师处理action
 * @author 古振猛 404879269@qq.com
 *
 */
public class TeacherAction extends ActionSupport {

	private static final long serialVersionUID = -2553194335437870957L;
	private TeacherService teacherService;
	private HashMap<String, Object> jsonMap = new HashMap<String, Object>();
	private String uid;
	private String upword;
	private int inrid;// INRID 编号 Int
	private String name;// Name 姓名 Nvarchar(10)
	private String gender;// Gender 性别 Nchar（1），标志”男女”
	private String university;// University 学校 Nvarchar（50）
	private String tchrid;// TchrID 教师编号 Nvarchar（50）
	private String hometown;// HomeTown 住址 Nvarchar（50）
	private String location;// Location 当前住址 Nvarchar（50）
	private String idnumber;// IDNumber 身份证号 char（20）
	private String duty;// Duty 职务 Nvarchar（20）
	private String phonenumber;// phoneNumber 电话 varchar（15）
	private String school;// School 学院 Nvarchar（50）
	private String email;// Email 邮箱 Nvarchar（50）
	private String manage;// Manage 管理班级 Nvarchar（255）
	// 标记方式：
	// “学位-专业名-年级-班级”如果管理两个或两个以上班级的话用管道符“|”隔开。学位号对应参照专业表的约定。
	private String remark;// Remark 备注 Nvarchar（1000）

	/**
	 * 教师登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {

		List<Teacher> teachers = this.teacherService.login(uid, upword);
		int size = teachers.size();
		if (size != 1) {
			jsonMap.put("result", 0);
		} else {
			jsonMap.put("result", 1);
			Teacher teacher = teachers.get(0);
			String teaherID = teacher.getTchrid();
			jsonMap.put("TeaherID", teaherID);
		}
		return INPUT;
	}
	/**
	 * 教师注册 
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {

		List<Teacher> teachers = this.teacherService
				.findTeacherByIdnumber(idnumber);
		if (teachers.size() > 0) {
			jsonMap.put("result", 0);
			return INPUT;
		}
		Teacher teacher = new Teacher(name, gender, university, tchrid,
				hometown, location, idnumber, duty, phonenumber, school, email,
				manage, remark);
		TeacherUsr teacherUsr = new TeacherUsr(uid, upword);
		int result = this.teacherService.insertTeacher(teacher, teacherUsr);
		if (result == 0) {
			jsonMap.put("result", 0);
		} else {
			jsonMap.put("result", 1);
		}
		return INPUT;
	}
	/**
	 * 教师更新信息
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		Teacher teacher = new Teacher(name, gender, university, tchrid,
				hometown, location, idnumber, duty, phonenumber, school, email,
				manage, remark);
		teacher.setInrid(inrid);
		TeacherUsr teacherUsr = new TeacherUsr(idnumber, upword);
		int result = this.teacherService.updateTeacher(teacher, teacherUsr);
		if (result == 0) {
			jsonMap.put("result", 0);
		} else {
			jsonMap.put("result", 1);
		}
		return INPUT;
	}
	/**
	 * 查看教师信息
	 * @return
	 * @throws Exception
	 */
	public String findTeacherByInrid() throws Exception {
		Teacher teacher = this.teacherService.findTeacherByInrid(inrid);
		if (teacher == null) {
			jsonMap.put("result", 0);
		} else {
			jsonMap.put("result", 1);
			jsonMap.put("teacher", teacher);
		}
		return INPUT;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public HashMap<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(HashMap<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getUpword() {
		return upword;
	}

	public void setUpword(String upword) {
		this.upword = upword;
	}

	public int getInrid() {
		return inrid;
	}

	public void setInrid(int inrid) {
		this.inrid = inrid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getUniversity() {
		return university;
	}

	public void setUniversity(String university) {
		this.university = university;
	}

	public String getTchrid() {
		return tchrid;
	}

	public void setTchrid(String tchrid) {
		this.tchrid = tchrid;
	}

	public String getHometown() {
		return hometown;
	}

	public void setHometown(String hometown) {
		this.hometown = hometown;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getIdnumber() {
		return idnumber;
	}

	public void setIdnumber(String idnumber) {
		this.idnumber = idnumber;
	}

	public String getDuty() {
		return duty;
	}

	public void setDuty(String duty) {
		this.duty = duty;
	}

	public String getPhonenumber() {
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getManage() {
		return manage;
	}

	public void setManage(String manage) {
		this.manage = manage;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

}
